package com.sec.android.app.myfiles.module.preview;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;

import com.sec.android.app.myfiles.info.FileType;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;

public class CompressFileRecord extends FileRecord {

    private FileRecord mArchiveRecord;

    public CompressFileRecord(FileRecord archiveRecord) {
        mArchiveRecord = archiveRecord;
        mStorageType = StorageType.Compress;
        mPath = "";
        mName = "";
    }

    public CompressFileRecord(FileRecord archiveRecord, String path, String name, long size, long date, int fileType, int isHidden,
                              int itemCount, int itemCountHidden) {
        super(StorageType.Compress, -1, path, name, size, date, fileType, isHidden, itemCount, itemCountHidden);
        if (mPath == null) {
            mPath = "";
        }
        this.mArchiveRecord = archiveRecord;
    }

    @Override
    public FileRecord getParent() {
        FileRecord ret = null;
        if (!isRoot()) {
            String parentPath = null;
            String parentName = null;
            if (!TextUtils.isEmpty(mPath)) {
                int index = mPath.lastIndexOf('/');
                if (index == -1) {
                    parentPath = "";
                    parentName = mPath;
                } else {
                    parentPath = mPath.substring(0, index);
                    parentName = mPath.substring(index + 1);
                }
            }
            ret = new CompressFileRecord(mArchiveRecord, parentPath, parentName, 0, 0, FileType.FOLDER, 0, 0, 0);
        } else {
            FileRecord parent = mArchiveRecord.getParent();
            if (parent != null) {
                ret = FileRecord.createFileRecord(StorageType.Local, parent.getFullPath());
            }
        }
        return ret;
    }

    @Override
    protected int _getId() {
        return 0;
    }

    @Override
    public String getName() {
        if (isRoot()) {
            return mArchiveRecord.getName();
        }
        return super.getName();
    }

    @Override
    public String getFullPath() {
        if (isRoot()) {
            return null;
        } else if (TextUtils.isEmpty(mPath)) {
            return mName;
        } else {
            return super.getFullPath();
        }
    }

    @Override
    public String getDisplayName(Context context) {
        return getName();
    }

    @Override
    public boolean isRoot() {
        return TextUtils.isEmpty(mPath) && TextUtils.isEmpty(mName);
    }

    public FileRecord getArchiveRecord() {
        return mArchiveRecord;
    }

    @Override
    protected Uri _getUri(Context context) {
        return null;
    }

    @Override
    public boolean exists(Context context) {
        boolean ret = false;
        if (mArchiveRecord != null) {
            ret = mArchiveRecord.exists(context);
        }
        return ret;
    }

    @Override
    public boolean equals(Object o) {
        boolean ret = false;

        if (o instanceof FileRecord) {
            FileRecord cmp = (FileRecord) o;
            if (cmp.getStorageType() == getStorageType()) {
                String fullPath = getFullPath();
                if (fullPath == null) {
                    ret = (cmp.getFullPath() == null);
                } else {
                    ret = fullPath.equals(cmp.getFullPath());
                }
            }
        } else {
            ret = (this == o);
        }
        return ret;
    }
}
